﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Distribution.Data;
using Distribution.General;
using Entities;

namespace Distribution.Service
{
    public class LTransformer
    {
        #region - Public methods -

        public static DataTable GetTransformerDetails(EPole pole)
        {
            return DTransformer.GetTransformerDetails(pole);
        }

        public static ETransformer GetById(int id)
        {
            return DTransformer.GetById(id);
        }

        public static void Save(ETransformer transformer)
        {
            DTransformer.Save(transformer);

            foreach (EImage i in transformer.Images)
            {
                FileInfo _file = new FileInfo(PPIConstants.K_ImagesFolder + i.TempFileName);

                if (_file.Exists && !i.IsDeleted && i.Id == Int32.MinValue)
                    i.FileName = _file.CopyTo(PPIConstants.K_ImagesFolder + i.FileName, true).Name;
            }
        }

        public static void Update(ETransformer transformer)
        {
            DTransformer.Update(transformer);

            foreach (EImage i in transformer.Images)
            {
                if (!i.FileName.Equals(i.TempFileName))
                {
                    FileInfo _file = new FileInfo(PPIConstants.K_ImagesFolder + i.TempFileName);

                    if (_file.Exists && !i.IsDeleted && i.Id == Int32.MinValue)
                        i.FileName = _file.CopyTo(PPIConstants.K_ImagesFolder + i.FileName, true).Name;
                }
            }
        }

        public static void Delete(EPole pole)
        {
            List<ETransformer> transformers = DTransformer.GetAll(pole);

            DTransformer.Delete(transformers);
        }

        public static void Delete(ETransformer transformer)
        {
            DTransformer.Delete(transformer);
        }

        public static void Delete(List<ETransformer> transformers)
        {
            DTransformer.Delete(transformers);
        }

        public static List<ETransformer> GetAll(EPole pole)
        {
            return DTransformer.GetAll(pole);
        }

        #endregion
    }
}
